package com.example.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.school.dto.AddressUpdateDto;
import com.example.school.dto.AdminGetListDto;
import com.example.school.dto.SaveAddressDto;
import com.example.school.pojo.Address;
import com.example.school.service.AddressService;
import com.example.school.mapper.AddressMapper;
import com.example.school.utils.common.enums.Code;
import com.example.school.utils.common.result.PageResponseResult;
import com.example.school.utils.common.result.Result;
import com.example.school.utils.config.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 24354
* @description 针对表【address】的数据库操作Service实现
* @createDate 2024-03-25 22:36:02
*/
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address>
    implements AddressService{

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Result getAdminList(AdminGetListDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        Page page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Address> wrapper=new LambdaQueryWrapper<>();
        if (dto.getMimo()!=null&&!dto.getMimo().equals("")) {
            wrapper.like(Address::getAddressName,dto.getMimo());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public Result saveAddress(SaveAddressDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Address address=new Address();
        BeanUtils.copyProperties(dto,address);
        int checkSave = addressMapper.insert(address);
        if (checkSave==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateAddress(AddressUpdateDto dto) {

        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Address checkAddress = addressMapper.selectById(dto.getId());
        if (checkAddress==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Address address=new Address();
        BeanUtils.copyProperties(checkAddress,address);
        if (dto.getName()!=null&&!dto.getName().equals("")) {
            address.setAddressName(dto.getName());
        }
        if (dto.getLongitude()!=null&&!dto.getLongitude().equals("")) {
            address.setAddressLongitude(dto.getLongitude());
        }
        if (dto.getLatitude()!=null&&!dto.getLatitude().equals("")) {
            address.setAddressLatitude(dto.getLatitude());
        }
        int checkUpdate = addressMapper.updateById(address);
        if (checkUpdate==0) {
            return Result.errorResult(Code.SERVER_ERROR);
        }

        return Result.okResult(Code.SUCCESS);
    }
}




